
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
 */
package meeple;

/**
 *
 * @author rogier
 */
public class MapTransform {
    private int jx_offset;
    private int jy_offset;
    private int width;

    public MapTransform(int x_screen_offset, int y_screen_offset, int image_width) {
        width = image_width;
        set_offset(x_screen_offset, y_screen_offset);
    }

    /**
     * @return the jx_offset
     */
    public int getJx_offset() {
        return jx_offset;
    }

    /**
     * @return the jy_offset
     */
    public int getJy_offset() {
        return jy_offset;
    }

    void set_offset(int x, int y) {
        jx_offset = x;
        jy_offset = y;
    }

    int getScreenX(int x) {
        return getJx_offset() + (width * x);
    }

    int getScreenY(int y) {
        return getJy_offset() + (width * y);
    }

    int getMapX(int x) {

//      return ((x/width)*width - jx_offset)/width;
        return floor(x - getJx_offset(), width);
    }

    int getMapY(int y) {

        // return ( jy_offset - (y/width)*width  )/width;
        return floor(y - getJy_offset(), width);
    }

    int SnapGridX(int x) {
        return getScreenX(getMapX(x));
    }

    int SnapGridY(int y) {
        return getScreenY(getMapY(y));
    }

    private int floor(int noemer, int deler) {
        if ((noemer > -deler) && (noemer < deler)) {
            return 0;
        } else {
            return (noemer / deler);
        }
    }
}


//~ Formatted by Jindent --- http://www.jindent.com
